angular.module('order-util', [])

.service 'orderService', ($ionicPopup, $http, $q, Toast)->
    {
        cancel: (id) ->
            deffer = $q.defer()
            myPopup = $ionicPopup.confirm({
                title: '是否要取消预约'
                template: '取消预约后仍需要继续服务需要重新下单哦！'
                okText: '确认'
                cancelText: '我再想想'
            })
            myPopup.then (res) ->
                if res
                    $http.put('/order/'+id+'/cancel',params: {})
                        .success (resp) ->
                            if resp.code == 0
                                Toast.showMsg("取消预约成功")
                                deffer.resolve()
                                console.log(resp)
                            else
                                console.log(resp)
                                Toast.showBusinessError(resp)
                        .error (resp, status) ->
                            console.log(resp)
                            Toast.showSysError(resp)
                else
                    console.log('cancel')

        ahead: (id) ->
            deffer = $q.defer()
            myPopup = $ionicPopup.confirm({
                title: '是否要提前结束服务'
                template: '提前结束后仍需要继续服务需要重新下单哦！'
                okText: '确认'
                cancelText: '取消'
            })
            myPopup.then((res) ->
                if res
                    $http.put("/order/#{id}/termination",params: {})
                        .success (resp) ->
                            if resp.code == 0
                                Toast.showMsg("申请提前终止成功")
                                deffer.resolve()
                                console.log(resp)
                            else
                                Toast.showBusinessError(resp)
                        .error (resp, status) ->
                            console.log(resp)
                            Toast.showSysError(resp)
                else
                    console.log('cancel')
            )

    }